﻿#建议保存编码为：bom头 + utf8
param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$prefix,

	[ValidateNotNullOrEmpty()]
	[String[]]$domains,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("cname")][String]$cluster名
)

& "${PSScriptRoot}/check_rds.ps1" -prefix $prefix -domains $domains -cluster名 $cluster名
if ($LASTEXITCODE -eq 0)
{
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：rds参数输入有错误。退出码1'
	exit 1
}

& "${PSScriptRoot}/_bak_rds.ps1"

& "${PSScriptRoot}/check_cds.ps1"  $cluster名
if ($LASTEXITCODE -eq 0)
{
	foreach ($temp011 in $domains)
	{
		New-Item -ItemType File -Path "${PSScriptRoot}/domains/${temp011}"
	}

	$prefix2 = $prefix.Replace('/','_')
	New-Item -ItemType File -Path "${PSScriptRoot}/prefix/${prefix2}"

	$private:rds_new = Get-Content -LiteralPath "${PSScriptRoot}/rds.json" | ConvertFrom-Json -Depth 99
	$private:temp003 = @($private:rds_new.resources | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)
	if ($private:temp003 -contains 'name')
	{
		$hash = @{
			'name'    = $cluster名

			'domains' = @($domains)

			'routes'  = @{

				'route' = @{
					'cluster' = $cluster名
				}

				'match' = @{
					'prefix' = $prefix
				}
			}
		}

		$private:rds = Get-Content -LiteralPath "${PSScriptRoot}/rds.json" | ConvertFrom-Json -Depth 99
		$private:rds.resources[0].virtual_hosts += @($hash)
		$private:rds2 = $private:rds | ConvertTo-Json -Depth 99
		Set-Content -LiteralPath "${PSScriptRoot}/rds.json" -Value $private:rds2 -Encoding utf8NoBOM
	}
	else
	{
		& "${PSScriptRoot}/Write-Error2.ps1"  '错误：请运行这个脚本【delall-rds-and-new-rds.ps1】。退出码3'
		exit 3
	}
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：找不到cluster名,或cluster名字错误。退出码2'
	exit 2
}

& "${PSScriptRoot}/_flashfile.ps1" "${PSScriptRoot}/rds.json"
& "${PSScriptRoot}/Write-Host2.ps1"  -ForegroundColor Green '信息：新建rds成功'
exit 0
